﻿using ITSM.Model.Entity;
using ITSM.Model.Model;
using ITSM.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ITSM.Controllers.User
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserInfoController : ControllerBase
    {
        private readonly ILogger<UserInfoController> _logger;
        private readonly IUserInfo _userInfo;
        private readonly IDemandTypeLink _typeLink;
        public UserInfoController(ILogger<UserInfoController> logger, IUserInfo userInfo,IDemandTypeLink typeLink)
        {
            _logger = logger;
            _userInfo = userInfo;
            _typeLink = typeLink;
        }
        [HttpGet]
        public IActionResult QueryUserId(string RYD_WxNo)
        {
            if (string.IsNullOrWhiteSpace(RYD_WxNo))
            {
                return NotFound();
            }

            string RYD_Id = _userInfo.QueryUserId(RYD_WxNo);
            if (RYD_Id=="")
            {
                return NotFound();
            }
            return Ok(RYD_Id);
        }

        [HttpGet("/api/GetCompany")]
        public IActionResult GetCompany(string RYD_UserId)
        {
            if (string.IsNullOrWhiteSpace(RYD_UserId))
            {
                return NotFound();
            }
            return Ok(_userInfo.GetCompanyUser(RYD_UserId));
        }

        [HttpGet("/api/GetUserInfo")]
        public IActionResult GetUserInfo(string RYD_UserId)
        {
            if (string.IsNullOrWhiteSpace(RYD_UserId))
            {
                return NotFound();
            }
            var userinfo = _userInfo.QueryUserInfo(RYD_UserId);
            return Ok(new { 
                userName=userinfo.RYD_Name,
                userPhone = userinfo.RYD_Phone,
                realName=userinfo.RealName
            });
        }

        [HttpGet("/api/GetState")]
        public IActionResult GetState(string RYD_UserId)
        {
            if (string.IsNullOrWhiteSpace(RYD_UserId))
            {
                return NotFound();
            }
            return Ok(_userInfo.GetStateUser(RYD_UserId));
        }
        [HttpPut("/api/setUserInfo")]
        public IActionResult SetUserInfo(RYD_UserInfo userInfo)
        {
            if (string.IsNullOrWhiteSpace(userInfo.RYD_Id)  || string.IsNullOrWhiteSpace(userInfo.RYD_Phone) || string.IsNullOrWhiteSpace(userInfo.RYD_Name))
            {
                return NotFound();
            }
            return Ok(_userInfo.UpdateUserInfo(userInfo));
        }

        [HttpPut]
        public IActionResult RegUserInfo(RYD_RegIt regIt)
        {
            if (string.IsNullOrWhiteSpace(regIt.RYD_UserId)||string.IsNullOrWhiteSpace(regIt.RYD_Company))
            {
                return NotFound();
            }
            RYD_RegIt rYD_RegIt = _userInfo.RegIt(regIt);
            if (string.IsNullOrWhiteSpace(rYD_RegIt.RYD_UserId))
            {
                return NotFound();
            }
            return Ok(_typeLink.SetDemandTypeLink(regIt.RYD_UserId, regIt.RYD_Company, regIt.RYD_DemandTypeNo));
        }
        [HttpPost("/api/GetAllUser")]
        public IActionResult GetAllUserInfo(RYD_UserInfo user)
        {
            if (string.IsNullOrWhiteSpace(user.RYD_Id)) {
                return Ok(_userInfo.GetAllName(Array.Empty<string>()));
            }
            return Ok(_userInfo.GetAllName(user.RYD_Id.Split(",")));
        }

    }
}
